
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NavigatorUtil {
  ///跳转页面
  static push(BuildContext context, Widget page) async {
    /* final result = await Navigator.push(
        context, MaterialPageRoute(builder: (context) => page));
    return result; */
    // return Navigator.push(context, CustomPageRoute());
    if(mCustomPage.isNotEmpty){
      if(mCustomPage.last.runtimeType!=page.runtimeType){
        mCustomPage.add(page);
      }
    }else{
      mCustomPage.add(page);
    }
    Navigator.push(
        context, CupertinoPageRoute(builder: (context) => page));
    // Navigator.push(context, FadeRoute(builder: (context) {
    //   return page;
    // }, barrierColor: Colors.transparent, barrierLabel: ''));
  }

  static popUntil(BuildContext context) async{
    Navigator.of(context).pushNamedAndRemoveUntil(
      '/',
          (route) => route == null,
    );
  }

  static pushNamed(
      BuildContext context, String routeName, Object arguments) async {
    /* final result = await Navigator.push(
        context, MaterialPageRoute(builder: (context) => page));
    return result; */
    return Navigator.pushNamed(context, routeName, arguments: arguments);
  }

  static List<Widget> mCustomPage = [];
  ///替换当前页面并跳转
  static replace(BuildContext context, Widget page) async {
    if(mCustomPage.isNotEmpty){
      if(mCustomPage.last.runtimeType!=page.runtimeType){
        mCustomPage.add(page);
      }
    }else{
      mCustomPage.add(page);
    }
    return Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (context) => page)
      // Platform.isIOS
      //     ? MaterialPageRoute(builder: (context) => page)
      //     : CustomPageRoute(page),
    );
  }

  ///返回上一个页面
  static pop(BuildContext context) async {
    if(mCustomPage.isNotEmpty){
      mCustomPage.removeLast();
    }
    return Navigator.of(context).pop();
  }

  ///清除自定义页面集合中的上一个页面
  static void removeCustomLastPage() {
    if (mCustomPage.isNotEmpty) {
      mCustomPage.removeLast();
    }
  }

  ///关闭所有页面
  static closeAllPage(BuildContext context, Widget page) async {
    if(mCustomPage.isNotEmpty){
      mCustomPage.clear();
    }
    return Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (context) => page),
      // Platform.isIOS
      //     ? MaterialPageRoute(builder: (context) => page)
      //     : CustomPageRoute(page),
          (route) => route == null,
    );
  }
}

class FadeRoute extends PageRoute {
  FadeRoute({
    required this.builder,
    this.transitionDuration = const Duration(milliseconds: 300),
    this.opaque = true,
    this.barrierDismissible = false,
    required this.barrierColor,
    required this.barrierLabel,
    this.maintainState = true,
  });

  final WidgetBuilder builder;

  @override
  final Duration transitionDuration;

  @override
  final bool opaque;

  @override
  final bool barrierDismissible;

  @override
  final Color barrierColor;

  @override
  final String barrierLabel;

  @override
  final bool maintainState;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) => builder(context);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(
      opacity: animation,
      child: builder(context),
    );
  }
}

